<template>
  <div class="login-container">
    <div class="login-box">
      <h1 class="login-title">智学云帆-教学管理系统</h1>
      
      <el-form :model="loginForm" :rules="loginRules" ref="loginFormRef" class="login-form">
        <el-form-item prop="username">
          <div class="input-label">
            <span class="required-mark">*</span>
            <span>用户名</span>
          </div>
          <el-input 
            v-model="loginForm.username" 
            placeholder="请输入员工用户名" 
            prefix-icon="User"
          />
        </el-form-item>
        
        <el-form-item prop="password">
          <div class="input-label">
            <span class="required-mark">*</span>
            <span>密 码</span>
          </div>
          <el-input 
            v-model="loginForm.password" 
            type="password" 
            placeholder="请输入密码" 
            prefix-icon="Lock"
            show-password
          />
        </el-form-item>
        
        <div class="login-buttons">
          <el-button type="primary" @click="handleLogin" :loading="loading" class="login-btn">登录</el-button>
          <el-button @click="resetForm" class="cancel-btn">取消</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import request from '../utils/request'

const router = useRouter()
const loginFormRef = ref(null)
const loading = ref(false)

// 登录表单数据
const loginForm = reactive({
  username: '',
  password: ''
})

// 表单验证规则
const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

// 处理登录
async function handleLogin() {
  if (!loginFormRef.value) return
  
  try {
    // 表单验证
    await loginFormRef.value.validate()
    
    loading.value = true
    
    // 发送登录请求
    const response = await request({
      url: '/login',
      method: 'post',
      data: loginForm
    })
    
    // 登录成功，保存token和用户信息
    localStorage.setItem('token', response.data.token)
    localStorage.setItem('userInfo', JSON.stringify({
      id: response.data.id,
      username: response.data.username,
      name: response.data.name
    }))
    
    ElMessage.success('登录成功')
    
    // 跳转到首页
    router.push('/')
  } catch (error) {
    console.error('登录失败:', error)
    ElMessage.error(error.message || '登录失败，请检查用户名和密码')
  } finally {
    loading.value = false
  }
}

// 重置表单
function resetForm() {
  if (loginFormRef.value) {
    loginFormRef.value.resetFields()
  }
}
</script>

<style scoped>
.login-container {
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(45deg, #6a11cb 0%, #2575fc 100%);
  animation: gradientAnimation 15s ease infinite;
  background-size: 400% 400%;
}

@keyframes gradientAnimation {
  0% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0% 50%;
  }
}

.login-box {
  width: 400px;
  background-color: #fff;
  border-radius: 10px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
  padding: 40px;
}

.login-title {
  font-size: 24px;
  color: #333;
  text-align: center;
  margin-bottom: 40px;
  font-weight: 500;
}

.login-form {
  width: 100%;
}

.input-label {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
}

.required-mark {
  color: #f56c6c;
  margin-right: 4px;
}

.login-buttons {
  display: flex;
  justify-content: space-between;
  margin-top: 30px;
}

.login-btn, .cancel-btn {
  width: 45%;
  height: 40px;
}

.login-btn:hover, .cancel-btn:hover {
  transform: translateY(-2px);
  transition: all 0.3s;
}

.login-btn {
  background: linear-gradient(to right, #4481eb, #04befe);
  border: none;
}

.login-btn:hover {
  background: linear-gradient(to right, #3671db, #03a9f4);
}

.cancel-btn {
  background: linear-gradient(to right, #e6e6e6, #ffffff);
  border: none;
  color: #606266;
}

.cancel-btn:hover {
  background: linear-gradient(to right, #d9d9d9, #f2f2f2);
  color: #409eff;
}

/* 输入框样式优化 */
:deep(.el-input__wrapper) {
  padding: 0 15px;
  height: 45px;
  box-shadow: 0 0 0 1px #dcdfe6 inset;
  transition: all 0.3s;
}

:deep(.el-input__wrapper:hover) {
  box-shadow: 0 0 0 1px #c0c4cc inset;
}

:deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px #409eff inset;
}
</style> 